package com.renwei.yunlong.activity.work;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.CommonSearchActivity;
import com.renwei.yunlong.adapter.CompanySelectAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CompanySelectBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SearchImage;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择企业
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class CompanySelectActivity extends BaseActivity implements HttpTaskListener, View.OnClickListener, BaseRecyclerViewAdapter.OnItemClickLitener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    @Bind(R.id.search_image)
    SearchImage searchImage;

    public static final int FROM_SENDER = 101; // 发起人
    public static final int FROM_DEALER = 102; // 处理人
    public static final int FROM_ASSIGNER = 103; // 指派人
    private CompanySelectAdapter adapter;
    private int FROM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_owner);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        FROM = getIntent().getIntExtra("from", 0);
        initView();
        initData();
    }

    private void initView() {
        if (FROM == FROM_SENDER) {
            simpleTileView.setTitle("选择业主企业");
        } else {
            simpleTileView.setTitle("选择处理企业");
        }
        searchImage.setOnClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CompanySelectAdapter(this, companyType);
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        if (FROM == FROM_SENDER) {
            map.put("useFlag", "1");
            map.put("type", "1");
            map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
            map.put("serverName", "");
            ServiceRequestManager.getManager().getWorkSendCompany(this, JsonMapUtil.mapToJson(map), this);
        } else {
            if (FROM == FROM_DEALER) {
                map.put("exeType", "1");
                map.put("serverName", "");
            } else if (FROM == FROM_ASSIGNER) {
                map.put("bindFlag", "1");
            }
            map.put("ownerCode", ownerBean.getRows().getOwnerCode());
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
            ServiceRequestManager.getManager().workOrderAssign(this, JsonMapUtil.mapToJson(map), this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 520) {
                setResult(RESULT_OK, data);
                CompanySelectActivity.this.finish();
            }
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        CompanySelectBean.Rows bean = adapter.getItem(position);
        Intent intent = new Intent();
        intent.putExtra("companyName", bean.getServerName());
        intent.putExtra("bindFlag", bean.getBindFlag());
        intent.putExtra("companyCode", bean.getCompanyCode());
        setResult(RESULT_OK, intent);
        CompanySelectActivity.this.finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_image:
                CommonSearchActivity.OpenActivityFroCompany(this, view, CompanySelectActivity.class, FROM);
                break;
            default:
                break;
        }
    }


    @Override
    public void onSuccess(int requestId, String result) {
        CompanySelectBean bean = new Gson().fromJson(result, CompanySelectBean.class);
        if (bean.getMessage().getCode() == 200) {
            adapter.setData(bean.getRows());
            if (bean.getRows() != null && bean.getRows().size() != 0) {
                stateLayout.showContentView();
            } else {
                stateLayout.showEmptyView();
            }
        }
    }

    @Override
    public void onException(int requestId, String code) {
        showTopWrongMsg(getResources().getString(R.string.net_work_error));
    }

    public static void OpenActivity(Object context, int from, int requestCode) {
        if (context instanceof Context) {
            Intent intent = new Intent((Context) context, CompanySelectActivity.class);
            intent.putExtra("from", from);
            ((Activity) context).startActivityForResult(intent, requestCode);
        } else if (context instanceof Fragment) {
            Intent intent = new Intent(((Fragment) context).getContext(), CompanySelectActivity.class);
            intent.putExtra("from", from);
            ((Fragment) context).startActivityForResult(intent, requestCode);
        }
    }

}
